package com.example.wjy.viewmodels;

import android.app.Application;

import com.example.room.entity.Master;
import com.example.room.repository.MasterRepository;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

/**
 * author: WangJunYan
 * package: com.example.wjy.viewmodels
 * date: 2020/6/7
 * time: 16:43
 * 用于处理MasterFragment中的数据，例如从数据库中获取数据
 */
public class MasterViewModel extends AndroidViewModel {
    /**
     * The list of masters using LiveData.
     */
    MutableLiveData<List<Master>> master;

    MasterRepository repository;

    public MasterViewModel(@NonNull Application application) {
        super(application);
        this.repository = new MasterRepository(application);
    }

    public LiveData<List<Master>> getMasterLive() {
        return repository.getMasters();
    }

    public void insertMasters(Master... masters) {
        repository.insertMaster(masters);
    }

    public void deleteAllMaster() {
        repository.deleteAllMaster();
    }

    public MutableLiveData<List<Master>> getMaster() {
        if(master == null) {
            master = new MutableLiveData<>();
            getMasterDataFromDataBase();
        }
        return master;
    }

    //用于测试能否从RecyclerView中显示数据，实际可能用不到，自行忽略
    public void getMasterDataFromDataBase() {
        List<Master> masters = new ArrayList<>();
//        for (int i = 0; i < 20;i++) {
//            Master master = new Master("111","nv");
//            masters.add(master);
//        }
        getMaster().setValue(masters);
    }
}
